package com.daxue.bootckafka.send;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

/**
 * @author daxue0929
 * @date 2022/10/24
 */

@Component
@Slf4j
public class TestMessageSender {

    public static final String TEST_TOPIC = "test-topic";

    public static final String TEST_TOPIC_GROUP_ID = "test-topic";

    final KafkaTemplate kafkaTemplate;

    @Autowired
    public TestMessageSender(KafkaTemplate kafkaTemplate) {
        this.kafkaTemplate = kafkaTemplate;
    }

    public void send(Object value) {
        ListenableFuture send = kafkaTemplate.send(TEST_TOPIC, JSON.toJSONString(value));
        send.addCallback(
            DefaultSendCallback.builder().serverName("testMessageSender").build()
        );
    }
}
